package bestfriends.tamu;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

//Read the xml documents using xstream

public class HighScores extends Activity {
	 int questionIndex;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.high_scores);
        SharedPreferences highScores = getSharedPreferences("highScores", MODE_WORLD_READABLE);
        questionIndex =  highScores.getInt("QUESTION", 0);
        
        TextView questionText = (TextView) findViewById(R.id.questionText);
        
        TextView firstPlace = (TextView) findViewById(R.id.textView1);
        TextView secondPlace = (TextView) findViewById(R.id.textView2);
        TextView thirdPlace = (TextView) findViewById(R.id.textView3);
        TextView runnerUp= (TextView) findViewById(R.id.textView4);
        
        Question q = ListManager.questions.get(questionIndex); 
        questionText.setText(q.getQuestionText());
        q.sortResults();
        //should not be out of bounds but could be with malformed xml
        Result rFirstPlace = q.getResult().get(0);
        Result rSecondPlace = q.getResult().get(1);
        Result rThirdPlace = q.getResult().get(2);
        Result rRunnerUp = q.getResult().get(3);
        
        int firstId = rFirstPlace.getId();
        int secondID = rSecondPlace.getId();
        int thirdID = rThirdPlace.getId();
        int runnerUpID = rRunnerUp.getId();
        
        
        for ( Person p : ListManager.people) {
        	if(p.getId() == firstId ) {
        		firstPlace.setText(p.getName());
        	} else if(p.getId() == secondID ) {
        		secondPlace.setText(p.getName());
        	} else if(p.getId() == thirdID ) {
        		thirdPlace.setText(p.getName());
        	}else if(p.getId() == runnerUpID ) {
        		runnerUp.setText(p.getName());
        	}
        }       
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.compare, menu);
        return true;
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	ListManager.writeArraysToSD();
    }
    
    public void loadCompare(View v) {
    	Intent i = new Intent(HighScores.this, Compare.class );
    	HighScores.this.startActivity(i);
    }
    
    public void prev(View v){
    	try{
    		SharedPreferences highScores = getSharedPreferences("highScores", MODE_WORLD_WRITEABLE);
    		SharedPreferences.Editor editor = highScores.edit();
    		questionIndex = (questionIndex - 1);
    		if(questionIndex < 0 ) {
    			questionIndex += ListManager.questions.size();
    		}
    		editor.putInt("QUESTION", questionIndex);
    		editor.commit();
    	} catch(Exception e){
    		e.printStackTrace();
    	}
    	Intent i = new Intent(HighScores.this, HighScores.class );
    	HighScores.this.startActivity(i);
    }
    
    public void next(View v){
    	try{
    		SharedPreferences highScores = getSharedPreferences("highScores", MODE_WORLD_WRITEABLE);
    		SharedPreferences.Editor editor = highScores.edit();
    		questionIndex = (questionIndex + 1);
    		if(questionIndex >= ListManager.questions.size() ) {
    			questionIndex -= ListManager.questions.size();
    		}
    		editor.putInt("QUESTION", questionIndex);
    		editor.commit();
    	} catch(Exception e){
    		e.printStackTrace();
    	}
    	Intent i = new Intent(HighScores.this, HighScores.class );
    	HighScores.this.startActivity(i);
    }
    
    
    /**
     * Launches the default email client from the device, only works on real phone
     * @param v
     */
    public void launchDefaultEmailClient(View v){
    	//this will only work for the files from sd card soo that is all.
    	ListManager.writeArraysToSD();
    	
    	//initialize paths to file
    	ArrayList<String> filePaths = new ArrayList<String>();
    	String base = Environment.getExternalStorageDirectory().getAbsolutePath();
		Toast.makeText(this, base, Toast.LENGTH_LONG).show();
    	filePaths.add(base + "/data.xml");
    	filePaths.add(base + "/questions.xml");
    	
    	final Intent i = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    	i.setType("message/rfc822");
    	i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
    	i.putExtra(Intent.EXTRA_SUBJECT, "My ratings");

        //has to be an ArrayList
        ArrayList<Uri> uris = new ArrayList<Uri>();
        //convert from paths to Android friendly Parcelable Uri's
        for (String file : filePaths)
        {
            File fileIn = new File(file);
            Uri u = Uri.fromFile(fileIn);
            uris.add(u);
        }
        i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);   	
    
    	try {
    	    startActivity(Intent.createChooser(i, "Send mail..."));
    	} catch (android.content.ActivityNotFoundException ex) {
    	    Toast.makeText(HighScores.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    	}
    }
    
    
}
